<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getJobList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button type="primary" :icon="CirclePlus" @click="insertDialogVisible = true">新增岗位</el-button>
      </template>
      <template #createDate="scope">
        {{ scope.row.createDate.replace("T", " ").split(".")[0] }}
      </template>
      <template #updateDate="scope">
        {{ scope.row.updateDate.replace("T", " ").split(".")[0] }}
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="EditPen" @click="openUpdateDialog(scope.row)">编辑</el-button>
        <el-button type="primary" link :icon="Delete" @click="updateJobStatus(scope.row, -1)">删除</el-button>
      </template>
    </ProTable>
    <!-- 新增对话框 -->
    <el-dialog width="40%" v-model="insertDialogVisible" title="新增岗位">
      <el-form :model="insertParam" :rules="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="岗位名称" prop="jobName">
              <el-input v-model="insertParam.jobName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序号码" prop="orderNum">
              <el-input-number v-model="insertParam.orderNum"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="岗位编码">
              <el-input v-model="insertParam.jobCode" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="insertParam.status">
                <el-radio :label="0">禁用</el-radio>
                <el-radio :label="1">正常</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注">
          <el-input v-model="insertParam.remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="insertDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="insertJob">新增</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 修改对话框 -->
    <el-dialog width="40%" v-model="updateDialogVisible" title="修改岗位">
      <el-form :model="updateParam" :rules="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="岗位名称" prop="jobName">
              <el-input v-model="updateParam.jobName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序号码" prop="orderNum">
              <el-input-number v-model="updateParam.orderNum"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="岗位编码">
              <el-input v-model="updateParam.jobCode" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="updateParam.status">
                <el-radio :label="0">禁用</el-radio>
                <el-radio :label="1">正常</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注">
          <el-input v-model="updateParam.remark" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="updateDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateJobInfo">修改</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts" name="jobManage">
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { CirclePlus, Delete, EditPen, Download, Upload, View, Refresh, ElementPlus } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { createJob, editJobInfo, editJobStatus, getJobList } from "@/api/modules/auth/job";
import { CreateJobParam, EditJobInfoParam, EditJobStatusParam, SysJob } from "@/api/interface/auth/job";
import { statusEnum, statusFieldNames } from "@/enums/httpEnum";

// ProTable 实例
const proTable = ref<ProTableInstance>();

const insertDialogVisible = ref(false);

const updateDialogVisible = ref(false);

/** 新增参数 */
const insertParam = reactive<CreateJobParam>({
  jobName: "",
  jobCode: "",
  orderNum: 0,
  status: 1
});

/** 修改参数 */
let updateParam = reactive<EditJobInfoParam>({
  jobId: "",
  jobName: "",
  jobCode: "",
  orderNum: 0,
  status: 1
});

const ruleForm = reactive<FormRules>({
  jobName: [{ required: true, message: "请输入岗位名称", trigger: "blur" }],
  orderNum: [{ required: true, message: "请输入排序号码", trigger: "blur" }]
});

const columns = reactive<ColumnProps<SysJob>[]>([
  { type: "selection", fixed: "left", width: 70 },
  { type: "index", label: "", width: 80 },
  {
    prop: "jobName",
    label: "岗位名称",
    search: { el: "input", tooltip: "请输入岗位名称" }
  },
  {
    prop: "jobCode",
    label: "岗位编码"
  },
  {
    prop: "orderNum",
    label: "排序"
  },
  {
    prop: "status",
    label: "状态",
    tag: true,
    enum: statusEnum,
    search: { el: "select", props: { filterable: true } },
    fieldNames: statusFieldNames
  },
  {
    prop: "createDate",
    label: "创建时间",
    width: 200
  },
  {
    prop: "updateDate",
    label: "修改时间",
    width: 200
  },
  { prop: "operation", label: "操作", fixed: "right", width: 330 }
]);

const openUpdateDialog = (param: SysJob) => {
  Object.assign(updateParam, param); // 保持响应式，只更新字段
  updateDialogVisible.value = true;
};

/** 修改部门信息 */
const updateJobInfo = async () => {
  await editJobInfo(updateParam);
  await proTable.value?.getTableList();
  updateDialogVisible.value = false;
  ElMessage.success("修改成功");
};

/** 修改部门状态 */
const updateJobStatus = async (param: SysJob, status: number) => {
  await editJobStatus({ jobId: param.jobId, status: status });
  await proTable.value?.getTableList();
  ElMessage.success("修改状态成功");
};

/** 新增部门 */
const insertJob = async () => {
  await createJob(insertParam);
  insertDialogVisible.value = false;
  await proTable.value?.getTableList();
  ElMessage.success("新增成功");
};
</script>
